package pers.amos.eshop.advance.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.amos.eshop.advance.product.common.DataTypeConstants;
import pers.amos.eshop.advance.product.common.EventTypeConstants;
import pers.amos.eshop.advance.product.mapper.CategoryMapper;
import pers.amos.eshop.advance.product.model.Category;
import pers.amos.eshop.advance.product.mq.RabbitMQSender;
import pers.amos.eshop.advance.product.service.CategoryService;


@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RabbitMQSender rabbitMQSender;

    public void add(Category category, String operationType) {
        categoryMapper.add(category);

        String sendMessage = RabbitMQSender.buildSendData(EventTypeConstants.ADD_EVENT_TYPE,
                DataTypeConstants.CATEGORY_DATA_TYPE, category.getId());

        String queue = rabbitMQSender.decideSendMegQueue(operationType);

        rabbitMQSender.send(queue, sendMessage);
    }

    public void update(Category category, String operationType) {
        categoryMapper.update(category);

        String sendMessage = RabbitMQSender.buildSendData(EventTypeConstants.UPDATE_EVENT_TYPE,
                DataTypeConstants.CATEGORY_DATA_TYPE, category.getId());

        String queue = rabbitMQSender.decideSendMegQueue(operationType);

        rabbitMQSender.send(queue, sendMessage);
    }

    public void delete(Long id, String operationType) {
        categoryMapper.delete(id);

        String sendMessage = RabbitMQSender.buildSendData(EventTypeConstants.DELETE_EVENT_TYPE,
                DataTypeConstants.CATEGORY_DATA_TYPE, id);

        String queue = rabbitMQSender.decideSendMegQueue(operationType);

        rabbitMQSender.send(queue, sendMessage);
    }

    public Category findById(Long id) {
        return categoryMapper.findById(id);
    }

}
